package org.example.crop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import org.example.crop.domain.CheckLog;
import org.example.crop.service.CheckLogService;
import org.example.loginAop.annotation.AnnAop;
import org.example.loginAop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/check")
@RestController
public class CheckController {
    @Autowired
    CheckLogService checkLogService;
    @RequestMapping("/list")
    public Result list(Integer pageNum) {
        PageHelper.startPage(pageNum,5);
        List<CheckLog> list = checkLogService.listall();
        return Result.success(new PageInfo<>(list));
    }
    @RequestMapping("/getnum")
    public Result getnum(){
        Integer inCount=checkLogService.inCountSum();
        Integer outCount=checkLogService.outCountSum();
        Integer diffCount=inCount-outCount;
        ArrayList<Integer> list =new ArrayList<>();
        list.add(inCount);
        list.add(outCount);
        list.add(diffCount);
        return Result.success(list);
    }
    @AnnAop("ceo")
    @RequestMapping("/updateById")
    public Result updateById(@RequestBody CheckLog checkLog){
        if(checkLog.getNum()==checkLog.getCountNum()){
            checkLog.setFlag(1);
            checkLog.setRemark("数据一致没有问题");
            checkLogService.updateById(checkLog);
            return Result.success();
        }
        checkLog.setRemark("盘点的数据与记录数据不一致");
        checkLog.setFlag(1);
        checkLogService.updateById(checkLog);
        return Result.success();
    }

    @RequestMapping("/exportOne")
    public void exportOne(Integer id, HttpServletResponse response){
        CheckLog checkLog = checkLogService.getById(id);
        ExcelUtil<CheckLog> excelUtil = new ExcelUtil<CheckLog>(CheckLog.class);
        excelUtil.exportExcel(response, Arrays.asList(checkLog), "盘点记录");
    }
}
